package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenlida
 * @date 2019/1/14 18:59
 * @description
 */
public class Code538 {
    private List<Integer> list = new ArrayList<>();

    public TreeNode convertBST(TreeNode root) {
        setList(root);
        setNode(root);
        return root;
    }

    private void setList(TreeNode node) {
        if (node != null) {
            list.add(node.val);
            setList(node.left);
            setList(node.right);
        }
    }

    private void setNode(TreeNode node) {
        if (node != null) {
            node.val = list.stream().filter(x -> x >= node.val).mapToInt(x -> x).sum();
            setNode(node.left);
            setNode(node.right);
        }
    }
}
